﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using PlayDeezGames.Common;
using System.Xml.Linq;

namespace DungeonDelver.Engine
{
    public class AtlasCell : DescriptoredEntityBase
    {
        public class Factory : IEntityFactory
        {
            public IEntity LoadFromXElement(IEntity theParent, System.Xml.Linq.XElement theXElement)
            {
                return new AtlasCell(theParent as AtlasColumn, theXElement);
            }

            public IEntity Create(IEntity theParent)
            {
                return new AtlasCell(theParent as AtlasColumn);
            }
            private Factory() { }

            public static void Register()
            {
                EntityFactories.RegisterFactory(Names.EngineFactories.AtlasCellFactory, new AtlasCell.Factory());
            }
            public static void Unregister()
            {
                EntityFactories.UnregisterFactory(Names.EngineFactories.AtlasCellFactory);
            }
        }
        private AtlasCell(AtlasColumn parent)
            : base(parent)
        {
        }

        private AtlasCell(AtlasColumn parent, XElement theXElement)
            : base(parent, theXElement)
        {
        }

        public AtlasColumn AtlasColumn
        {
            get
            {
                return ParentEntity as AtlasColumn;
            }
        }
        public Game Game
        {
            get
            {
                return AtlasColumn != null ? AtlasColumn.Game : null;
            }
        }

        public int Row
        {
            get
            {
                if (AtlasColumn != null)
                {
                    foreach (var row in AtlasColumn.Rows)
                    {
                        if (AtlasColumn[row] == this)
                        {
                            return row;
                        }
                    }
                }
                throw new IndexOutOfRangeException();
            }
        }

        private Dictionary<int, Map> maps = new Dictionary<int, Map>();

        public Dictionary<int, Map>.KeyCollection Layers
        {
            get
            {
                return maps.Keys;
            }
        }

        public Map this[int index]
        {
            get
            {
                if (maps.ContainsKey(index))
                {
                    return maps[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (value == null)
                {
                    maps.Remove(index);
                }
                else
                {
                    maps[index] = value;
                }
            }
        }


        protected override XElement OnToXElement()
        {
            XElement result = new XElement(Names.EngineXNames.AtlasCell);
            result.Add(new XAttribute(PlayDeezGames.Common.Names.CommonXNames.Factory, Names.EngineFactories.AtlasCellFactory));
            result.Add(new XAttribute(Names.EngineXNames.Row, Row.ToString()));
            return result;
        }

        protected override void OnFromXElement(XElement theXElement)
        {
            base.OnFromXElement(theXElement);
            AtlasColumn[int.Parse(theXElement.Attribute(Names.EngineXNames.Row).Value)] = this;
        }

        public static AtlasCell Create(AtlasColumn theParent)
        {
            return EntityFactories.Create(theParent, Names.EngineFactories.AtlasCellFactory) as AtlasCell;
        }
        public static AtlasCell Load(AtlasColumn theParent, XElement theXElement)
        {
            return EntityFactories.Load(theParent, theXElement) as AtlasCell;
        }
    }
}
